using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace PocketAbapa
{
	public class HouseList
	{
		public HouseList(Field mField)
		{
			field = mField;
			int size = mField.FieldSize;

			list = new House[2 * size];

			for (int i = 0; i < field.FieldSize; i++)
			{
				House newButton = CreateHouse(i, field.DefaultNumberOfBalls);
				this[i] = newButton;

				newButton = CreateHouse(size + i, field.DefaultNumberOfBalls);
				this[size + i] = newButton;
			}
		}

		public House this[int index]
		{
			get
			{
				return list[index];
			}
			set
			{
				list[index] = value;
			}
		}

		private House[] list;

		private Field field;

		public void Reset()
		{
			foreach (House h in list)
			{
				h.Balls = field.DefaultNumberOfBalls;
			}
			Enable();
		}

		public void Enable()
		{
			bool enableFirst = field.Players.CurrentPlayerIndex == 0;
			for (int i = 0; i < field.FieldSize; i++)
			{
				this[i].Enable(enableFirst);
				this[i + field.FieldSize].Enable(!enableFirst);
			}
		}

		private House CreateHouse(int Index, int Balls)
		{
			House result = new House(Index, Balls);
			result.Font = new Font("Times New Roman", 14, FontStyle.Bold);
			result.Click += new EventHandler(ButtonClick);
			result.Visible = true;
			field.Controls.Add(result);
			return result;
		}

		void ButtonClick(object sender, EventArgs e)
		{
			House house = sender as House;
			if (house != null)
			{
				field.HouseClicked(house);
			}
		}
	}
}
